require.config({
	paths:{
		"jquery":"jquery-1.11.3",
		"cookie":"jquery.cookie"
	}
})
require(["jquery","cookie"],function($,cookie){
	$(function(){
		//给登陆按钮设置事件
		$(".login-btn").click(function(){
			//获取用户名和密码
			var user = $(".userName").val();
			var pwd = $(".userPsd").val();
			//检验用户名和密码是否正确
			//获取cookie中的用户信息
			var users = $.cookie("registerUsers") ? $.cookie("registerUsers") : "";
			//将字符串转为对象
			users = convertStrToObj(users);
			if(users[user] == pwd){
				//登陆成功
				$.cookie("loginedUsers",user,{expires:7,path:"/"});
				location.href = "../index.html";
			}else{
				alert("用户名与密码不匹配，请确认后重试");
			}
		});
		$(".toRegister").click(function(){
			//转到注册页
			location.href = "../html/register.html";
		});
	});
//	将字符串转为对象
	function convertStrToObj(str){
		if(!str){
			return{};
		}
		var users = str.split(":");
		var obj = {};
		for(var i = 0; i < users.length; i ++){
			var userData = users[i].split(",");
			obj[userData[0]] = userData[1];//对象的
		}
		return obj;
	}
//	将对象转为字符串
	function convertObjToStr(obj){
		var str = "";
		for(var user in obj){
			var pwd = obj[user];
			if(str){
				str += ":";
			}
			str += user + "," + pwd;
		}
		return str;
	}
})






